package com.zp.self.module.level_1_问道.III_多线程.JUC;

import java.util.concurrent.locks.LockSupport;

class MyThread extends Thread {
    private Object object;
    public MyThread(Object object) {
        this.object = object;
    }

    public void run() {
        System.out.println("before unpark");
        try {
            Thread.sleep(1000);
            // 获取blocker
            System.out.println("Blocker info " + LockSupport.getBlocker((Thread) object));
            // 释放许可
            LockSupport.unpark((Thread) object);
            // 休眠500ms，保证先执行park中的setBlocker(t, null);
            Thread.sleep(500);
            // 再次获取blocker
            System.out.println("Blocker info " + LockSupport.getBlocker((Thread) object));
            System.out.println("after unpark");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

public class LockSupportTest2 {
    public static void main(String[] args) {
        MyThread myThread = new MyThread(Thread.currentThread());
        myThread.start();
        System.out.println("before park");
        // 获取许可
        LockSupport.park("ParkAndUnparkDemo");
        System.out.println("after park");
    }
}
